package com.vodafone.mapreduce.config.xml;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

import com.vodafone.mapreduce.config.beans.DatabaseConfiguration;


/**
 * Bean definition parser for handling the reporting database configuration
 * 
 * @author $Author$
 * @version $Id$
 * @since 1.0-SNAPSHOT
 */
public class DatabaseConfigurationBeanDefinitionParser extends
		AbstractSingleBeanDefinitionParser {

	private static final String JDBC_FETCH_SIZE = "jdbc-fetch-size";
	private static final String JPA_FETCH_SIZE = "jpa-fetch-size";
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
	 * #doParse(org.w3c.dom.Element,
	 * org.springframework.beans.factory.support.BeanDefinitionBuilder)
	 */
	@Override
	protected void doParse(Element element, BeanDefinitionBuilder builder) {
		builder.addConstructorArgValue(element.getAttribute("uri"));
		builder.addConstructorArgValue(element.getAttribute("login"));
		builder.addConstructorArgValue(element.getAttribute("password"));
		builder.addConstructorArgValue(element.getAttribute("dialect"));

		if (element.hasAttribute(JDBC_FETCH_SIZE)) {
			builder.addPropertyValue("jdbcFetchSize", Integer.parseInt(element.getAttribute(JDBC_FETCH_SIZE)));
		}
		if (element.hasAttribute(JPA_FETCH_SIZE)) {
			builder.addPropertyValue("jpaFetchSize", Integer.parseInt(element.getAttribute(JPA_FETCH_SIZE)));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser
	 * #getBeanClass(org.w3c.dom.Element)
	 */
	@Override
	protected Class<DatabaseConfiguration>getBeanClass(Element element) {
		return DatabaseConfiguration.class;
	}

}
